﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using _430GroupProject;
using Model;

namespace WebClient
{
    public partial class UserHours : System.Web.UI.Page
    {
        private ArrayList _data = new ArrayList(); //swap for array by ID/Team
        private ClockInSession editing;

        public DateTime PayPeriodStart
        {
            get
            {
                if (DateTime.Now.Day <= 15)
                {
                    return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                }
                else
                {
                    return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16);
                }
            }

        }

        public DateTime PayPeriodEnd
        {
            get
            {
                if (DateTime.Now.Day <= 15)
                {
                    return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);
                }
                else
                {
                    return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

                }
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }

            UserName.Text = (Session["user"] as User).NetID;

            

        }

       

        protected void SubmitHours(object sender, EventArgs e)
        {
            if (!TotalHours.Text.Equals(""))
            {
                User user = (User) Session["user"];
                double hoursLogged=-1;
                try
                {
                    hoursLogged = Convert.ToDouble(TotalHours.Text);
                    if(hoursLogged > 40 ||hoursLogged <1)
                    {
                        throw new FormatException();
                    }
                }catch(FormatException fe)
                {
                    ErrorMessage.Text = "Please enter a number Between 1 and 40 for you work Hours.";
                    return;//Not in the correct format.
                }

                WorkSession toAdd = new WorkSession(user.NetID, DateTime.Now, hoursLogged, PayPeriodStart, PayPeriodEnd);
                
                Master.Proxy.addWorkSession(toAdd);
                Response.Redirect(Request.RawUrl);



            }
        }

       

       
        

        protected void LoadNewData(object sender, EventArgs e)
        {

            ViewState["selectedTeam"] = (string)TeamSelect.SelectedValue;
        }

        private void BindData()
        {
            CurrentHours.DataSource = _data;
        }

        private void GatherData()
        {
            //GetDataItem worksession given users and workgroups
           // _data.Add(new WorkSession("UserNet", DateTime.Now, 24));
            
        }

        protected void SendTimeSheet(object sender, EventArgs e)
        {
            User user = (User)Session["user"];
            WorkSessions hours = Master.Proxy.userWorkSessions(user.NetID, PayPeriodStart, PayPeriodEnd);
            WorkGroup team = Master.Proxy.getWorkGroup(ViewState["selectedTeam"] as string);
            TimeSheet ts = new TimeSheet(user, team, PayPeriodStart, PayPeriodEnd);
            ts.WorkSessions = hours.Sessions;
            Master.Proxy.addTimeSheet(ts);
            Master.Proxy.submitTimeSheet(ts);
            Response.Redirect(Request.RawUrl);
            
            
               
            
        }

        protected void TeamSelect_Load(object sender, EventArgs e)
        {
            TeamSelect.EnableViewState = true;
            User user = (User)Session["user"];
            if (user != null && user.WorksIn != null && user.WorksIn.Count > 0)
            {
                List<string> toManage = new List<string>();
                if (user.Permissions.Contains(Permission.Admin))
                {
                    toManage = Master.Proxy.getAllWorkgroupNames();
                }
                else
                {
                    toManage = user.WorksIn;
                }

                TeamSelect.DataSource = toManage;
                TeamSelect.DataBind();

                if (!IsPostBack)
                {
                    ViewState["selectedTeam"] = (string)TeamSelect.SelectedValue;
                }
                LoadDataGrid(TeamSelect.SelectedValue);
            }
            else
            {

            }
        }

        private void LoadDataGrid(string teamName)
        {
            WorkSessions hrsLogged = new WorkSessions();
            User user = (User) Session["user"];
            hrsLogged = Master.Proxy.userWorkSessions(user.NetID, PayPeriodStart, PayPeriodEnd);
            CurrentHours.DataSource = hrsLogged.Sessions;
            CurrentHours.DataBind();
        }

        protected void ErrorClose_Click(object sender, EventArgs e)
        {
            ErrorPanel.Visible = false;
        }
    }
}